package com.DJ.controller;

import com.dj.common.dto.QueryDto;
import com.dj.common.Code;
import com.dj.common.Result;
import com.dj.common.domain.UserVO;
import com.DJ.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RequestMapping("/user")
@RestController
@jdk.nashorn.internal.runtime.logging.Logger
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //增
    @PostMapping(value = "/save")
    public Result save(@RequestBody UserVO userVO){
        userService.insert(userVO);
        return new Result("添加成功");
    }

    //删
    @PostMapping(value = "/{uuid}")
    public Result delete(@PathVariable Long uuid){
        userService.delete(uuid);
        return new Result("添加成功");
    }

    //改
    @PostMapping(value = "/update")
    public Result update(@RequestBody UserVO userVO){
        userService.update(userVO);
        return new Result("添加成功");

    }

    //查
    @GetMapping(value = "/getOne/{id}")
    public Result getOne(@PathVariable Long id){
        UserVO userVO = userService.get(id);
        return new Result(userVO);
    }

    //分页查询
    @PostMapping (value = "/getAll")
    public Result getAll(@RequestBody QueryDto dto){
        Page<UserVO> userList = userService.getAll(dto);
        log.info("userList: {}",userList);
        return new Result(Code.GET_SUCCESS.getCode(),userList,"查询成功");
    }

    //文件上传
    @PostMapping(value = "/upload")
    public Result upload(@RequestParam(value = "files")MultipartFile multipartFile) {
        //获取原始文件名
        String OriginalFileName = multipartFile.getOriginalFilename();
        //获取文件内容类型可以根据 类型和文件名 进行上传类型控制
        String contentType = multipartFile.getContentType();
        try {
            userService.file(multipartFile);
            return new Result(Code.GET_SUCCESS.getCode(),"文件上传成功");
        }catch (IOException e){
            return new Result("文件上传失败");
        }
    }

    /*
    * 报表数据导出
    * */
    @RequestMapping("/api/export")
    public void exportExcel(HttpServletRequest request) {
        userService.exportExcel(request);
    }

    /*
     * 报表数据导入
     * */
    @RequestMapping("/api/import")
    public void importExcel(@RequestParam(value = "files")  MultipartFile file) {
        userService.importExcel(file);
    }

//    *//**
//     * 文件预览
//     *//*
//
//    *//**
//     * 文件下载
//     *//*
//
//    @PostMapping("/upload")
//    public Result download(){
//        return userService.download();
//    }*/
}
